package com.msb.consumer.listener;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.AMQP;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * @author sunpeng
 * @Date 2021-08-11 16:08
 */

@Component
public class RabbitmqListener {

    @RabbitListener(queues = "myQueue")
    public void demo1(String msg){
        System.out.println("demo1获取到的内容："+msg);
    }

    @RabbitListener(queues = "myQueue")
    public void demo2(String msg){
        System.out.println("demo2获取到的内容："+msg);
    }

    @RabbitListener(queues = "myfanout1")
    public void demo3(String msg){
        System.out.println("demo3获取到myfanout1的内容："+msg);
    }

    @RabbitListener(queues = "myfanout1")
    public void demo4(String msg){
        System.out.println("demo4获取到myfanout1的内容："+msg);
    }

    @RabbitListener(queues = "myfanout2")
    public void demo5(String msg){
        System.out.println("demo5获取到myfanout2的内容："+msg);
    }

    //@RabbitListener(queues = "topicQueue1")
    public void demo6(String msg){
        System.out.println("demo6获取到topicQueue1的内容："+msg);
    }

    //@RabbitListener(queues = "topicQueue2")
    public void demo7(String msg, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag){
        if("false".equals(msg)){
            System.out.println("手动确认false: "+msg);
            int i = 1/0;
        }else if("true".equals(msg)){
            System.out.println("手动确认true: "+msg);
            try {
                channel.basicAck(tag, false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            System.out.println("手动确认其他: "+msg);
        }
    }

    @RabbitListener(queues = "topicQueue2")
    public void demo8(String msg){
        if("false1".equals(msg)){
            System.out.println("false1: "+msg);
            int i = 1/0;
        }else if("false2".equals(msg)){
            System.out.println("false2: "+msg);
            int i = 1/0;
        }
        System.out.println("正常: "+msg);
    }

}
